/**
  ******************************************************************************
  * @file    bsp_watchdog.c
  * @author  Iron
  * @date    2018-08-08
  * @version v1.0
  * @brief   bsp_watchdog c file, MAX823
  */

/** @addtogroup GROUP_BSP
  * @{
  */

/* includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "bsp_watchdog.h"

/* private typedef -----------------------------------------------------------*/
/* private define ------------------------------------------------------------*/

#ifdef APP_RELEASE_VERSION
    /* watchdog config */
    #define BSP_ENABLE_IWDT      // DISABLE FOR DEBUG
#else
    #warning "watchdog not enable!"
#endif

/* private macro -------------------------------------------------------------*/
/* private variables ---------------------------------------------------------*/
IWDG_HandleTypeDef hiwdg;

/* private function prototypes -----------------------------------------------*/
/* private functions ---------------------------------------------------------*/
extern void _Error_Handler(char *file, int line);


void bsp_watchdog_flash_earse_init(void)
{
#ifdef BSP_ENABLE_IWDT
    HAL_IWDG_Refresh(&hiwdg);
#endif
}

void bsp_watchdog_flash_earse_deinit(void)
{
    bsp_watchdog_keepalive();

#ifdef BSP_ENABLE_IWDT
    HAL_IWDG_Refresh(&hiwdg);
#endif
}

void bsp_watchdog_flash_earse_keepalive(void)
{
#ifdef BSP_ENABLE_IWDT
    HAL_IWDG_Refresh(&hiwdg);
#endif
}

/**
  * @brief  update iwg timer, keep mcu alive.
  * @param  None
  * @retval None
  */
void bsp_watchdog_keepalive(void)
{
#ifdef BSP_ENABLE_IWDT
    HAL_IWDG_Refresh(&hiwdg);
#endif
}

/**
  * @brief  Independent watchdog
  * @param  None.
  * @retval None.
  */
int32_t bsp_watchdog_init(void)
{
#ifdef BSP_ENABLE_IWDT
    hiwdg.Instance = IWDG;
    hiwdg.Init.Prescaler = IWDG_PRESCALER_32;  // 4096ms, flash section (128k) erase max timeout 4s
    hiwdg.Init.Reload = 4095;
    if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
#endif
    return 0;
}

/**
  * @}
  */

/******************* (C)COPYRIGHT 2018 ***** END OF FILE *********************/
